package cn.wolfcode.luowowo;

import cn.wolfcode.luowowo.website.interceptor.CheckLoginInterceptor;
import cn.wolfcode.luowowo.website.web.resolve.UserInfoResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.WebContentInterceptor;

import java.util.List;

@SpringBootApplication
@ServletComponentScan
public class WebSite implements WebMvcConfigurer {
    @Bean
    public CheckLoginInterceptor checkLoginInterceptor() {
        return new CheckLoginInterceptor();
    }

    @Bean
    public UserInfoResolver userInfoResolver() {
        return new UserInfoResolver();
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add( userInfoResolver() );
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor( checkLoginInterceptor() ).addPathPatterns( "/**" )
                .excludePathPatterns( "/userLogin" )
                .excludePathPatterns( "/userRegist" )
                .excludePathPatterns( "/sendVerifyCode" )
                .excludePathPatterns( "/checkPhone" )
                .excludePathPatterns( "/login.html" )
                .excludePathPatterns( "/regist.html" )
                .excludePathPatterns( "/image/**" )
                .excludePathPatterns( "/js/**" )
                .excludePathPatterns( "/styles/**" );
    }

    public static void main(String[] args) {
        SpringApplication.run( WebSite.class, args );
    }

}
